package com.hailiao.beidou.message.ui.page.connect

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.d10ng.app.managers.PermissionManager
import com.d10ng.compose.model.UiViewModelManager
import com.d10ng.compose.ui.sheet.builder.RadioSheetBuilder
import com.hailiao.beidou.comm.serialport.SPWire
import com.hailiao.beidou.comm.serialport.constant.BaudRate
import com.hailiao.beidou.comm.serialport.constant.DataBits
import com.hailiao.beidou.comm.serialport.constant.Parity
import com.hailiao.beidou.comm.serialport.constant.StopBits
import com.hailiao.beidou.message.bd.Beidou
import com.hailiao.beidou.message.bd.constant.BDStatus
import com.hailiao.beidou.message.bd.sp.SPMode
import com.hailiao.beidou.message.data.setting.SettingRepository
import com.hailiao.beidou.message.data.store.data.SPSettingData
import com.hailiao.beidou.message.utils.back
import com.hailiao.beidou.message.utils.storagePermissionArray
import kotlinx.coroutines.launch

/**
 * 串口连接配置页
 * @Author d10ng
 * @Date 2024/3/7 10:29
 */
class SPConnectScreenViewModel: ViewModel() {

    // 选择串口模式
    val modeFlow = SettingRepository.spModeFlow
    // 串口自定义配置
    val settingFlow = SettingRepository.spSettingFlow

    init {
        Beidou.disconnect()
        viewModelScope.launch {
            PermissionManager.request(storagePermissionArray)
        }
    }

    /**
     * 选择模式
     * @param value SPMode
     */
    fun onChangeMode(value: SPMode) {
        SettingRepository.setSPMode(value)
    }

    /**
     * 更新设置
     * @param value SPSettingData
     */
    fun onUpdateSetting(value: SPSettingData) {
        SettingRepository.setSPSetting(value)
    }

    /**
     * 点击波特率
     */
    fun onClickBaudRate() {
        UiViewModelManager.showSheet(RadioSheetBuilder(
            title = "波特率",
            items = BaudRate.entries.toSet(),
            itemText = { it.intValue.toString() },
            selectedItem = settingFlow.value.rdBaudRate,
            showButton = false,
            onConfirmClick = {
                onUpdateSetting(settingFlow.value.copy(rdBaudRate = it))
                true
            }
        ))
    }

    /**
     * 点击数据位
     */
    fun onClickDataBits() {
        UiViewModelManager.showSheet(RadioSheetBuilder(
            title = "数据位",
            items = DataBits.entries.toSet(),
            itemText = { it.intValue.toString() },
            selectedItem = settingFlow.value.rdDataBits,
            showButton = false,
            onConfirmClick = {
                onUpdateSetting(settingFlow.value.copy(rdDataBits = it))
                true
            }
        ))
    }

    /**
     * 点击检验类型
     */
    fun onClickParity() {
        UiViewModelManager.showSheet(RadioSheetBuilder(
            title = "检验类型",
            items = Parity.entries.toSet(),
            itemText = { it.name },
            selectedItem = settingFlow.value.rdParity,
            showButton = false,
            onConfirmClick = {
                onUpdateSetting(settingFlow.value.copy(rdParity = it))
                true
            }
        ))
    }

    /**
     * 点击停止位
     */
    fun onClickStopBits() {
        UiViewModelManager.showSheet(RadioSheetBuilder(
            title = "停止位",
            items = StopBits.entries.toSet(),
            itemText = { it.intValue.toString() },
            selectedItem = settingFlow.value.rdStopBits,
            showButton = false,
            onConfirmClick = {
                onUpdateSetting(settingFlow.value.copy(rdStopBits = it))
                true
            }
        ))
    }

    /**
     * 点击打开串口
     */
    fun onClickOpen() {
        UiViewModelManager.showLoading()
        viewModelScope.launch {
            val w = modeFlow.value.wire ?: SPWire(settingFlow.value.toSPConfig())
            if (Beidou.connectSP(w)) {
                Beidou.onDeviceConnected(SettingRepository.protoFlow.value)
                UiViewModelManager.hideLoading()
                Beidou.statusFlow.value = BDStatus.CONNECTING
                back()
            } else {
                UiViewModelManager.hideLoading()
                UiViewModelManager.showErrorNotify("连接串口失败")
                Beidou.statusFlow.value = BDStatus.DISCONNECT
                Beidou.disconnect()
            }
        }
    }
}